from django.contrib.contenttypes.fields import GenericRelation
from django.db import models
from django.shortcuts import reverse
from django.utils.translation import gettext_lazy as _
from audit.models import AuditManagement
from lib.django import models as mymodels
from .project import Project
from lib import constants
from lib.django import models as mymodels

from .lib import ProjectSubMixin

class Plan(ProjectSubMixin, models.Model):
    project = models.ForeignKey(
        verbose_name=_("项目"),
        to=Project,
        on_delete=models.CASCADE,
        blank=True
    )

    time = models.DateField(
        verbose_name=_("时间"),
        auto_now_add=True # won't shown in form
    )

    title = models.CharField(
        verbose_name=_("日程"),
        max_length=constants.TITLE_FIELD_LENGTH,
    )
    
    content = mymodels.WangEditorNextField(
        verbose_name=_("内容摘要"),
        blank=True,
        default=list
    )

    note = models.TextField(
        verbose_name="备注信息",
        blank=True,
        default="",
    )

    is_draft = mymodels.YesNoField(
        verbose_name=_("草稿"),
        blank=True,
        help_text=_("不显示在审核列表"),
        default=False,
    )

    # note that this field must be explit set (set in a parent class is of no use)
    # test on django rest framework api
    audit_management = GenericRelation(
        AuditManagement,
        content_type_field='content_type',
        object_id_field='object_id',
        verbose_name=_("审核管理")
    )


    class Meta:
        default_permissions=[]
        db_table="project_plan"
        verbose_name=_("项目方案")
        verbose_name_plural= verbose_name

    @classmethod
    def get_link(cls, project_id, id):
        return reverse("project:plan/detail", kwargs={'project_pk': project_id, 'pk': id})
